package com.ugrow.ugrowcommunity.activity.campus;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.bean.StudentBean;
import com.ugrow.ugrowcommunity.data.Data;
import com.ugrow.ugrowcommunity.databinding.ActivityStudentCampusCardBinding;
import com.ugrow.ugrowcommunity.util.QRCodeUtil;

import java.util.Random;

public class CampusCardActivity extends AppCompatActivity {
    private ActivityStudentCampusCardBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityStudentCampusCardBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setData();
        //返回退出
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //付款码
        binding.paymentCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View dialog = View.inflate(CampusCardActivity.this, R.layout.dialog_payment_code, null);
                ImageView cancel = dialog.findViewById(R.id.cancel);
                ImageView QR_code = dialog.findViewById(R.id.qr_code);
                TextView money = dialog.findViewById(R.id.money);
                money.setText(binding.money.getText());
                AlertDialog alertDialog = new AlertDialog.Builder(CampusCardActivity.this)
                        .setView(dialog)
                        .show();
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });
                Bitmap mBitmap = QRCodeUtil.createQRCodeBitmap(binding.money.getText().toString(), 200, 200);
                QR_code.setImageBitmap(mBitmap);
            }
        });
    }

    private void setData() {
        Random rand = new Random();
        int i = rand.nextInt(100) + 1;
        binding.money.setText(i+".00");
        StudentBean studentBean = Data.StudentBean;
        binding.tvCardName.setText(studentBean.getData().get(0).getStuName());
        binding.tvCardStuNum.setText(studentBean.getData().get(0).getStuNum());
        binding.tvCardFaculty.setText(studentBean.getData().get(0).getFaculty());
        binding.tvCardMajor.setText(studentBean.getData().get(0).getClazz());
        Glide.with(CampusCardActivity.this).load(studentBean.getData().get(0).getStuPhoto()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(binding.image);

    }

}